<?php

namespace app\modules\api\controllers;

use yii;
use app\constants\Code;
use yii\web\Controller;
use app\helpers\XSafe;
use app\services\YapiSafe;
use yii\web\Response;

class BaseController extends Controller
{
    //接口控制器屏蔽csrf校验  表单才使用
    public $enableCsrfValidation = false;
    public $control_cache_key;


    public function init()
    {
        parent::init();

        //验证Token
        //YapiSafe::checkHttpHeaders();
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }
    
    /**
     * 生成接口交互请求的token
     * @param array $requestParams
     * @return string
     * @author chenym
     * @since ：2017-07-04
     */
    protected function createRequestToken($requestParams = [])
    {
        if (empty($requestParams)) {
            return false;
        }
        $postJson = json_encode($requestParams);
        $time     = date('YmdHis', time());
        $jsonSalt = Yii::$app->params['apiKeySalt'];
        $mark     = $postJson . '###' . $time . '###' . $jsonSalt;
        $token    = XSafe::STD3Encrypt($postJson . '###' . $time . '###' . md5($mark), 'apiDesc');
        return $token;
    }

    /**
     * 校验对应的请求的token
     * @param string $requestStr
     * @author chenym
     * @since 2017-07-04
     */
    protected function checkRequestToken($requestStr = '')
    {
        $decRes   = XSafe::STD3Decrypt(urldecode($requestStr));
        $jsonSalt = Yii::$app->params['apiKeySalt'];
        if (empty($requestStr) || !$decRes) {
            die($this->ajaxReturn(Code::API_REQUEST_TOKEN_ERROR, Code::$messages[Code::API_REQUEST_TOKEN_ERROR]));
        } else {
            $paramsArr = explode('###', $decRes);
            if (md5($paramsArr[0] . '###' . $paramsArr[1] . '###' . $jsonSalt) != $paramsArr[2]) {
                die($this->ajaxReturn(Code::API_REQUEST_TOKEN_ERROR, Code::$messages[Code::API_REQUEST_TOKEN_ERROR]));
            }
        }
    }

    /**
     * 获取版本缓存key
     * @param $keyname
     */
    public function getCacheModuleKey($keyname)
    {
        $keyval = time();
        //初始化删除缓存的标识，给后台管理进行删除
        if (Yii::$app->cache->get($keyname) === false) {
            Yii::$app->cache->set($keyname, $keyval, 0);
        }
        $this->control_cache_key = Yii::$app->cache->get($keyname);
    }
    /**
     * 返回成功
     * @param string $msg 提示信息
     * @param array $data 数据
     * @return string 输出json信息
     */
    public function rspSuccess($msg = "ok",$data = null)
    {
        $res = ["status" => 1,"message" => $msg];
        $data && $res["data"] = $data;
        $response = Yii::$app->response;
        $response->format = Response::FORMAT_JSON;
        $response->data = $res;
        $response->send();
    }
    /**
     * 返回错误
     * @param string $errCode 错误code
     * @param string $errMsg 提示信息
     * @return string 输出json信息
     */
    public function rspError($errCode,$errMsg = null)
    {
        $res = ["status" => $errCode,"message" => $errMsg];
        $response = Yii::$app->response;
        $response->format = Response::FORMAT_JSON;
        $response->data = $res;
        $response->send();
    }
}
